package com.ds.infrastructure.hyperspace.console.model.request.handler;

import com.ds.infrastructure.hyperspace.console.config.validation.EnumValue;
import com.ds.infrastructure.hyperspace.console.dto.handler.HandlerParamsDTO;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbHandlerEnum;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbPublicEnum;
import com.ds.infrastructure.hyperspace.console.entity.table.TbHandler;
import com.ds.infrastructure.hyperspace.console.utils.FastJsonUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-09-24 17:48
 **/
@Data
@NoArgsConstructor
public class SaveHandlerForm {
    private String handlerName;
    @ApiModelProperty(value = "Handler类型:1=preFlow;2=postFlow;3=preFlow|postFlow(通用处理器);", example = "3")
    @EnumValue(TbHandlerEnum.HANDLER_TYPE.class)
    private Integer handlerType;
    @NotEmpty
    private List<@Valid HandlerParamsForm> handlerParams;
    @ApiModelProperty(value = "描述说明", example = "")
    private String description;
    @ApiModelProperty(value = "示例", example = "")
    private String example;

    public static TbHandler toEntity(SaveHandlerForm form) {
        TbHandler tbHandler = new TbHandler();
        tbHandler.setHandlerName(form.getHandlerName());
        tbHandler.setHandlerType(form.getHandlerType());
        tbHandler.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        List<HandlerParamsDTO> handlerParamsDTOList = new ArrayList<>();
        form.getHandlerParams().forEach(item -> handlerParamsDTOList.add(HandlerParamsForm.toDTO(item)));
        String handlerParamsJson = FastJsonUtil.serialize(handlerParamsDTOList);
        tbHandler.setHandlerParams(handlerParamsJson);
        tbHandler.setDescription(form.getDescription());
        tbHandler.setExample(form.getExample());
        return tbHandler;
    }

    public static TbHandler toEntity4SelectOne(SaveHandlerForm form) {
        TbHandler tbHandler = new TbHandler();
        tbHandler.setHandlerName(form.getHandlerName());
        tbHandler.setHandlerType(form.getHandlerType());
        tbHandler.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        return tbHandler;
    }
}
